/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.product;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.modules.ivm.dao.product.ProductClassifyDao;
import com.neusoft.lohas.modules.ivm.entity.product.ProductClassify;
import com.neusoft.lohas.modules.ivm.utils.ProductUtils;

/**
 * 商品分类Service
 * @author Jackie.Liu
 * @version 2014-10-28
 */
@Component
@Transactional(readOnly = true)
public class ProductClassifyService extends BaseService {

	@Autowired
	private ProductClassifyDao productClassifyDao;
	
	public ProductClassify get(String id) {
		return productClassifyDao.get(id);
	}
	
	public Page<ProductClassify> find(Page<ProductClassify> page, ProductClassify productClassify) {
		DetachedCriteria dc = productClassifyDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(productClassify.getName())){
			dc.add(Restrictions.like("name", "%"+productClassify.getName()+"%"));
		}
		dc.add(Restrictions.eq(ProductClassify.FIELD_DEL_FLAG, ProductClassify.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("createBy", productClassify.getCreateBy()));
		dc.addOrder(Order.asc("sort"));
		return productClassifyDao.find(page, dc);
	}
	
	public List<ProductClassify> findAllList(ProductClassify productClassify) {
		DetachedCriteria dc = productClassifyDao.createDetachedCriteria();
		dc.add(Restrictions.eq(ProductClassify.FIELD_DEL_FLAG, ProductClassify.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("merchant.id", productClassify.getMerchant().getId()));
		dc.addOrder(Order.asc("sort"));
		return productClassifyDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ProductClassify productClassify) {
		if (productClassify.getParent()!=null) {
			productClassify.setParent(this.get(productClassify.getParent().getId()));
		}
		productClassifyDao.clear();
		productClassifyDao.save(productClassify);
		ProductUtils.removeCache(ProductUtils.CACHE_CLASSIFY_LIST);
		ProductUtils.removeCache(ProductUtils.CACHE_CLASSIFY_ONE_LIST);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		productClassifyDao.deleteById(id);
		ProductUtils.removeCache(ProductUtils.CACHE_CLASSIFY_LIST);
		ProductUtils.removeCache(ProductUtils.CACHE_CLASSIFY_ONE_LIST);
	}

	public ProductClassify findByName(String name, String mId) {
		return productClassifyDao.findByName(name, mId);
	}
	
	//删除子类
	@Transactional(readOnly = false)
	public void deleteChildrens(String id) {
		productClassifyDao.deleteChildrens(id);
		ProductUtils.removeCache(ProductUtils.CACHE_CLASSIFY_LIST);
		ProductUtils.removeCache(ProductUtils.CACHE_CLASSIFY_ONE_LIST);
	}
	
}
